from django.shortcuts import render

# Create your views here.
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from app_path.models import Path, Stage, User_Path
from app_path.serializers import PathSer, StageSer
from course.models import Course
from course.views import PageNum


class ApppathView(ModelViewSet):
    # def perform_authentication(self, request):
    #     pass
    queryset = Path.objects.all().order_by('id')
    serializer_class = PathSer
    permission_classes = (AllowAny,)
    #     # 指定过滤方法类, 排序方法类, 一个或多个
    #
    #     filter_backends = (DjangoFilterBackend, OrderingFilter)  # 同时支持过滤和排序
    #
    #     # 指定排序字段, 不设置, 排序功能不起效
    #     ordering_fields = ('follow_num', 'create_time')
    #
    #     # 指定过滤字段, 不设置, 过滤功能不起效
    #     filter_fields = ('title', 'category', 'tag', 'status')
    #
    #     # 自定义分页器 覆盖全局配置
    # pagination_class = PageNum


# class StageView(ModelViewSet):
#     queryset = Stage.objects.all()
#     serializer_class = StageSer
#     permission_classes = (AllowAny,)


class PathStageView(APIView):
    permission_classes = (AllowAny,)

    # def perform_authentication(self, request):
    #     pass
    def get(self, request):
        pid = request.GET.get("pid")
        queryset = Path.objects.get(id=pid)
        pathdata = PathSer(queryset).data  # 该路径的详细信息
        stages = Stage.objects.filter(path=pid)
        data_list = []
        for i in stages:
            data = {}
            data['title'] = i.title
            data['numb'] = i.numb
            data['id'] = i.id
            data["coursedata"] = []
            # courses = Course.objects.filter(stage__course__stage__in=[i.id,])
            courses = Course.objects.filter(stage=i.id)
            for j in courses:
                dict = {}
                dict['id'] = j.id
                dict['title'] = j.title
                dict['img'] = str(j.img)
                dict['follow_num'] = j.follow_num
                dict['category'] = j.category.title
                data['coursedata'].append(dict)
            data_list.append(data)
        return Response({"code": 200, "msg": "所有数据", "pathdata": pathdata,
                         "stagedata": data_list})


class User_Path_View(APIView):
    permission_classes = (AllowAny,)

    # def perform_authentication(self, request):
    #     pass
    def get(self, request):
        pid = request.GET.get("pid")
        uid = request.GET.get("uid")
        count = User_Path.objects.filter(user=uid, path=pid).count()
        print(count)
        if count > 0:
            return Response({"type": '1'})
        else:
            return Response({"type": '0'})


class FollowView(APIView):
    permission_classes = (AllowAny,)

    # def perform_authentication(self, request):
    #     pass
    def get(self, request):
        pid = request.GET.get("pid")
        uid = request.GET.get("uid")
        type = request.GET.get("type")
        print(pid, uid, type)
        if not all([pid, uid, type]):
            return Response({"code": 400, "msg": "参数不全"})
        if type == "0":
            userpath = User_Path.objects.filter(user_id=uid, path_id=pid).delete()
        else:
            userpath = User_Path(user_id=uid, path_id=pid)
            userpath.save()
        return Response({"code": 200, "msg": "关注或取消成功"})
